package com.hugy.test.rabbitmq02;

import com.hugy.test.basic.RabitMQConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import static com.hugy.test.basic.Constants.FANOUT_EXCHANGE_NAME;


/**
 * @author 蚂蚁课堂创始人-余胜军QQ644064779
 * @title: ExProducer
 * @description: 每特教育独创第五期互联网架构课程
 * @date 2019/12/521:51
 */
public class ExProducerFanout {

    /**
     *  fanout 交换机
     */
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        System.out.println("生产者启动成功..");
        // 1.创建我们的连接
        Connection connection = RabitMQConnection.getConnection();
        // 2.创建我们通道
        Channel channel = connection.createChannel();
        // 不需要直接关心队列，只关心交换机
        channel.exchangeDeclare(FANOUT_EXCHANGE_NAME, "fanout", true);
        for (int i = 0; i < 10; i++) {
            String msg = "生产者群发消息"+i;
            channel.confirmSelect();
            channel.basicPublish(FANOUT_EXCHANGE_NAME, "", null, msg.getBytes());
            if (channel.waitForConfirms()) {
                System.out.println("生产者发送消息成功:" + msg);
            }
        }
        channel.close();
        connection.close();
        // 如果交换机没有绑定队列，消息可能会丢失
    }
}
